package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeService;
import com.github.pagehelper.PageInfo;

import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 员工管理控制器
 * 处理员工相关的HTTP请求
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    /**
     * 查询所有员工信息
     * @return Result对象，包含员工列表数据
     */
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Employee> list = employeeService.selectAll();
        return Result.success(list);
    }

    /**
     * 分页查询员工信息
     * @param pageNum 页码
     * @param pageSize 每页显示数量
     * @return Result对象，包含分页后的员工列表数据
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam(defaultValue = "1") int pageNum, 
                                @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Employee> list = employeeService.selectByPage(pageNum, pageSize);
        return Result.success(list);
    }

    /**
     * 根据ID查询员工信息
     * @param id 员工ID
     * @return Result对象，包含单个员工数据
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Employee employee = employeeService.selectById(id);
        return Result.success(employee);
    }

    /**
     * 添加新员工
     * @param employee 员工对象，包含姓名、性别、年龄、描述信息
     * @return Result对象，包含操作结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee){
        int rows = employeeService.add(employee);
        if(rows > 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 更新员工信息
     * @param employee 员工对象，包含ID、姓名、性别、年龄、描述信息
     * @return Result对象，包含操作结果
     */
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee){
        int rows = employeeService.update(employee);
        if(rows > 0){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     * 批量删除员工信息
     * @param ids 员工ID列表
     * @return Result对象，包含操作结果
     */
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        int rows = employeeService.deleteByIds(ids);
        if(rows > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 删除员工信息
     * @param id 员工ID
     * @return Result对象，包含操作结果
     */
    @DeleteMapping("/deleteById/{id}")
    public Result delete(@PathVariable Integer id){
        int rows = employeeService.delete(id);
        if(rows > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

}
